package com.tocean.demo.service;

import com.tocean.demo.dto.ExaminationSet;
import com.tocean.demo.entity.questionbank.HisTestRecord;
import com.tocean.demo.entity.questionbank.HisTestScore;
import com.tocean.demo.repository.HisTestRecordRepository;
import com.tocean.demo.util.SnowflakeIdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class HisTestRecordService {

    @Autowired
    private SnowflakeIdWorker snowflakeIdWorker;

   @Autowired
    private HisTestRecordRepository hisTestRecordRepository;

    /**
     * 添加历史做题情况
     * @param examinationSet
     */
   public void AddTestRecord(ExaminationSet examinationSet , String sequenceId){
       int righhtAnswer = 0;
       int wrongAnswer  = 0;
       for(int m=0;m<examinationSet.getMyQuestions().size();m++){
           if(examinationSet.getMyQuestions().get(m).getIsCorrect())
               righhtAnswer++;
           else if(!examinationSet.getMyQuestions().get(m).getIsCorrect())
               wrongAnswer++;
       }
       for(int i=0 ;i<examinationSet.getMyQuestions().size();i++) {
           HisTestRecord hisTestRecord = new HisTestRecord();
           hisTestRecord.setId(snowflakeIdWorker.nextId());
           hisTestRecord.setUserId(examinationSet.getUserId());
           hisTestRecord.setSequenceId(sequenceId);
           hisTestRecord.setContent(examinationSet.getMyQuestions().get(i).getContent());
           hisTestRecord.setCid(examinationSet.getMyQuestions().get(i).getId());
           hisTestRecord.setChoice(examinationSet.getMyQuestions().get(i).getChoice());
           hisTestRecord.setRAnswer(examinationSet.getMyQuestions().get(i).getRAnswer());
           hisTestRecord.setRightcount(righhtAnswer);
           hisTestRecord.setWrongcount(wrongAnswer);

           hisTestRecordRepository.save(hisTestRecord);

       }
   }


    /**
     * 查找历史做题情况
     */
    public List<HisTestRecord> selectRecord(HisTestScore hisTestScore){
        List<HisTestRecord> hisTestRecords = hisTestRecordRepository.findAllByUserIdAndSequenceId(hisTestScore.getUserId(),hisTestScore.getSequenceId());
        return hisTestRecords;
    }

}
